<?php
	session_start();
	$id = $_SESSION['user_id'];
	include "../common/db.inc";
	$mysqli = new mysqli($host, $username, $password,$dbname);
	$query = "call sp_GetUserOnline('$id')";
	$result = $mysqli->query($query);
	if(mysqli_num_rows($result))
	{
		header('Location: ../user/home.php');
		exit();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>EbookAZ</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<script type="text/javascript" src="../scripts/login.js"></script>
	<?php require "../components/head.php" ?>
</head>
<body onload="MakeCurrentPage(1)">
<div id="container">
	<?php require "../components/guest_menu.php" ?>
	<?php require "../components/guest_header.php" ?>
	<div id="content">
		<?php require "../components/content_left.php"?>
		<div id="content_right">
		<?php
		include "db.inc";

		$mysqli = new mysqli($host, $username, $password,$dbname);
		$mysqli->set_charset('utf8');	
		/* check connection */
		if (mysqli_connect_errno()) {
		    printf("Connect failed: %s\n", mysqli_connect_error());
		    exit();
		}


		$query = ("call sp_GetMostDownload('0','8')");

		echo "<h1 style='text-align:center'>Ebook được down nhiều nhất</h1>";
		if ($result = $mysqli->query($query)) 
		{
			$count =  mysqli_num_rows($result);
			echo "<table cellspacing='6' class='showbook'>";
			$i = 0;
		    while ($row = $result->fetch_row()) 
			{
				$title = $row[1];
				$img = $image.$row[8];	
				$mana = $row[13];
				$downloadcount = $row[11];
				if($i%4 == 0)
					echo "<tr>";
				echo "<td width='25%' valign='top'><a href='XemChiTietSach.php?id=$row[0]'><img src='$img' width='100px' height='150px' alt='EBook'/></a><br/>";
				echo "<a href='XemChiTietSach.php?id=$row[0]'><b>$title</b></a><br/>";
				echo "<label><i>Mana: </i>$mana</label><br/>";
				echo "<label><i>Download: </i>$downloadcount lần</label><br/>";
				echo "<a href='XemChiTietSach.php?id=$row[0]'>Xem chi tiết</a>";
				echo "</td>";
				if($i % 4 == 3 || $i == $count-1)
					echo "</tr>";
				$i++;
			}
		    /* free result set */
		    $result->close();
		}
		echo "</table>";	

		$mysqli->close();


		echo "<br/>";
		echo "<br/>";
		echo "<br/>";
		$mysqli = new mysqli($host, $username, $password,$dbname);

		/* check connection */
		if (mysqli_connect_errno()) {
		    printf("Connect failed: %s\n", mysqli_connect_error());
		    exit();
		}
		$mysqli->set_charset('utf8');		
		$query = ("call sp_GetNewUpload('0','8')");

		echo "<h1 style='text-align:center'>Ebook mới nhất</h1>";
		if ($result = $mysqli->query($query)) 
		{
			$count =  mysqli_num_rows($result);
			echo "<table cellspacing='6' class='showbook'>";
			$i = 0;
		    while ($row = $result->fetch_row()) 
			{
				$title = $row[1];
				$author = $row[5];
				$img = $image.$row[8];	
				$mana = $row[13];
				$date = $row[12];
				$arr = date_parse($date);
				$ngay = $arr['day'];
				$thang = $arr['month'];
				$nam = $arr['year'];
				if ($ngay < 10)
					$ngay = "0" . $ngay;
				if ($thang < 10)
					$thang = "0" . $thang;
				$date = $ngay . "/" . $thang . "/" . $nam;
				
				if($i%4 == 0)
					echo "<tr>";
				echo "<td width='25%' valign='top'><a href='XemChiTietSach.php?id=$row[0]'><img src='$img' width='100px' height='150px' alt='EBook'/></a><br/>";
				echo "<a href='XemChiTietSach.php?id=$row[0]'><b>$title</b></a><br/>";
				echo "<label><i>Mana: </i>$mana</label><br/>";
				echo "<label>$date</label><br/>";
				echo "<a href='XemChiTietSach.php?id=$row[0]'>Xem chi tiết</a>";
				echo "</td>";
				if($i % 4 == 3 || $i == $count)
					echo "</tr>";
				$i++;				
			}
		    /* free result set */
		    $result->close();
		}
		echo "</table>";	
		$mysqli->close();
		?>
		</div>
	</div>
	<?php require "../components/footer.php" ?>
</div>
</body>
</html>
